package com.woniuxy.mqConsumer.listener;

import com.woniuxy.common.enums.RabbitEnum;
import com.woniuxy.mqConsumer.entity.Room;
import com.woniuxy.mqConsumer.service.OrderService;
import com.woniuxy.mqConsumer.service.RoomService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.swing.*;

@Component

public class RoomListener {
    @Autowired
    RoomService roomService;
    @Autowired
    OrderService orderService;

    /**
     * 通过房间状态判断消费者的处理流程
     * 状态为准备中则改为正在竞拍，
     * 正在竞拍改为已结束
     * @param room
     */
    @RabbitListener(queues = RabbitEnum.DEAD_QUEUE)
    @RabbitHandler
    public void updateRoomStatus(@Payload Room room){
        System.err.println(room.getId());
        try {
            if(room.getStatus() == 0) {
                //倒计时结束，将房间状态改为正在竞拍,进入竞拍倒计时
                System.err.println("正在竞拍");
                roomService.startRoomStatus(room);
            } else if (room.getStatus() == 1) {
                //倒计时结束，将房间状态改为已结束
                System.err.println("已结束");
                roomService.endRoomStatus(room);
            }
        }catch (RuntimeException e){
            e.printStackTrace();
        }
    }

    /**
     * 先查询该订单状态，如果是未付款则将订单改为违约并扣去保证金
     * @param ordersId
     */
    @RabbitListener(queues = RabbitEnum.ORDER_DEAD_QUEUE)
    @RabbitHandler
    public void updateOrderStatus(@Payload int ordersId){
        orderService.updateOrderStatus(ordersId);
    }
}
